package org.linys.action.sale;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.linys.action.BaseAction;
import org.linys.model.sale.Sale;
import org.linys.model.sale.SaleDetail;
import org.linys.model.sale.SaleFee;
import org.linys.model.system.Operator;
import org.linys.service.sale.SaleDetailServ;
import org.linys.service.sale.SaleFeeServ;
import org.linys.service.sale.SaleServ;
import org.linys.util.DateUtil;
import org.linys.util.OperateUtil;
import org.linys.util.PageUtil;
import org.linys.util.ResponseMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings({  "unchecked" })
@Controller("SaleAction")
@Scope("prototype")
public class SaleAction extends BaseAction implements ModelDriven{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Sale sale= new Sale();
	private ResponseMsgUtil responseMsgUtil;
	private PageUtil pageUtil;
	private SaleServ saleServ;
	private SaleDetailServ saleDetailServ;
	private SaleFeeServ saleFeeServ;
	private OperateUtil  operateUtil;
	private List<SaleFee> saleFeeList;
	private List<SaleDetail> saleDetailList;
	
	
	public void viewTempSaleDetail(){
		List<SaleDetail> saleDetailList = new ArrayList<SaleDetail>();
		JSONArray jsonArray = JSONArray.fromObject(saleDetailList);
		JSONObject object = new JSONObject();
		object.put("total", 10);
		object.put("rows", jsonArray);
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	public void save(){
		try{
		JSONObject object = new JSONObject();
		preData();
		Operator currentOperator=operateUtil.getOperator();
		sale.setHandlerID(currentOperator.getOperatorID_());
		if(saleServ.save(sale)>0){
			object.put("success", 1);
			object.put("saleID", sale.getSaleID());
			operateUtil.setModelOperateTime(OperateUtil.SESSION_SALE_OPERATETIME, new Date());
		}
		this.responseMsgUtil.doResponse(object.toString());
		}catch(Exception e){
			e.printStackTrace();
			
		}
	}
	
	@SuppressWarnings("static-access")
	public void update(){
		try{
		//1.保存订单的数据
		//2.将修改的订单详细情况的数据，保存到临时表中，后调用存储过程比较真实表和临时表中的差别，并将数据保存真实的订单详细表
		JSONObject object = new JSONObject();
		preData();
		sale.setOperateTime(DateUtil.dateToString(operateUtil.getModelOperateTime(OperateUtil.SESSION_SALE_OPERATETIME)));
		if(saleServ.update(sale)>0){
			object.put("success", 1);
		}
		this.responseMsgUtil.doResponse(object.toString());
		}catch(Exception e){
			e.printStackTrace();
			
		}
	}
	
	/**
	 * 根据saleID取得对应的托运单费用，托运单货物明细和托运单的详细信息
	 * @return
	 */
	public String  preUpdate(){
		operateUtil.setModelOperateTime(OperateUtil.SESSION_SALE_OPERATETIME, new Date());
		this.saleFeeList = this.saleFeeServ.getBySaleID(sale.getSaleID());
		this.saleDetailList = this.saleDetailServ.getBySaleID(sale.getSaleID());
		this.sale = this.saleServ.getBySaleID(sale.getSaleID());
		this.request.setAttribute("saleDetailList", saleDetailList);
		this.request.setAttribute("saleFeeList", saleFeeList);
		return "preUpdate";
	}
	
	private void preData(){
		String goodsIDStr = request.getParameter("goodsIDStr");
		String sizeStr = request.getParameter("sizeStr");
		String weightStr = request.getParameter("weightStr");
		String qtyStr = request.getParameter("qtyStr");
		String unitStr = request.getParameter("unitStr");
		String volumeStr = request.getParameter("volumeStr");
		String grossWeightStr = request.getParameter("grossWeightStr");
		
		String feeItemIDStr = request.getParameter("feeItemIDStr");
		String amountStr = request.getParameter("amountStr");
		String noteStr = request.getParameter("noteStr");
		
		
		Operator currentOperator=operateUtil.getOperator();
		sale.setOperatorID(currentOperator.getOperatorID_());
		//货物
		if(goodsIDStr != null && goodsIDStr != null && !"".equals(goodsIDStr)){
			String goodsIDArray[] = goodsIDStr.split("\\^\\^");
			String sizeArray[] = sizeStr.split("\\^\\^");
			String weightArray[] = weightStr.split("\\^\\^");
			String qtyArray[] = qtyStr.split("\\^\\^");				
			String unitArray[] = unitStr.split("\\^\\^");
			String volumeArray[] = volumeStr.split("\\^\\^");
			String grossWeightArray[] = grossWeightStr.split("\\^\\^");
			
			SaleDetail detail = null;				
			for(int i = 0 ; i < qtyArray.length ; i++){
				detail = new SaleDetail();
				detail.setSaleID(sale.getSaleID());
				detail.setGoodsID(new Integer(goodsIDArray[i]));						
				detail.setSize(sizeArray[i]);
				detail.setWeight(new Double(weightArray[i]));
				detail.setQty(new Double(qtyArray[i]));
				detail.setUnitID(new Integer(unitArray[i]));
				detail.setVolume(new Double(volumeArray[i]));
				detail.setGrossWeight(new Double(grossWeightArray[i]));
				detail.setOperatorID(currentOperator.getOperatorID_());
				sale.getSaleDetailList().add(detail);
			}
		}
		//费用
		if(feeItemIDStr != null && feeItemIDStr != null && !"".equals(feeItemIDStr)){
			String feeItemIDArray[] = feeItemIDStr.split("\\^\\^");
			String amountArray[] = amountStr.split("\\^\\^");
			String noteArray[] = noteStr.split("\\^\\^");
			
			SaleFee saleFee= null;
			for(int i = 0 ; i < feeItemIDArray.length ; i++){
				saleFee = new SaleFee();
				saleFee.setSaleID(sale.getSaleID());
				saleFee.setFeeItemID(new Integer(feeItemIDArray[i]));	
				saleFee.setAmount(new Double(amountArray[i]));
				saleFee.setNote(noteArray[i]);
				saleFee.setOperatorID(currentOperator.getOperatorID_());
				sale.getSaleFeeList().add(saleFee);
			}
		}
	}
	
	public void view() {
		JSONObject object = new JSONObject();
		try{
			this.pageUtil.cal(saleServ.count(sale), sale.getRows(), sale.getPage());
			List<Sale> sales = saleServ.view(sale, this.pageUtil.getStart(), this.pageUtil.getRows());
			
			JSONArray jsonArray = JSONArray.fromObject(sales);
			object.put("total", this.pageUtil.getTotal());
			object.put("rows", jsonArray);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.responseMsgUtil.doResponse(object.toString());
		}
	}
	
	
	/**
	 * 批量删除托运单
	 */
	public void deleteByIDs() {
		String[] idsStr = sale.getIds().split(",");
		for (String id : idsStr) {
			try {
				saleServ.deleteByID(new Integer(id));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		JSONObject object = new JSONObject();
		object.put("success", 1);
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	
	public void updateStatus() {
		JSONObject object = new JSONObject();
		Operator currentOperator=operateUtil.getOperator();
		sale.setOperatorID(currentOperator.getOperatorID_());
		sale.setOperateTime(DateUtil.dateToString(operateUtil.getModelOperateTime(OperateUtil.SESSION_SALE_OPERATETIME)));
		if(saleServ.updateStatus(sale)>0){
			object.put("success", 1);
		}
		this.responseMsgUtil.doResponse(object.toString());
	}
	
	/***
	 * 批量修改状态
	 */
	public void updateStatusByIDs(){
		try{
		JSONObject object = new JSONObject();
		Operator currentOperator=operateUtil.getOperator();
		sale.setOperatorID(currentOperator.getOperatorID_());
		sale.setOperateTime(DateUtil.dateToString(new Date()));
		if(saleServ.updateStatusByIDs(sale)>0){
			object.put("success", 1);
		}
		this.responseMsgUtil.doResponse(object.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/***
	 * 验证托运单编号是否重复
	 */
	public void validateCode(){
		try{
		if(saleServ.validateCode(sale)==0){
			this.responseMsgUtil.doResponse("true");
		}
		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	public ResponseMsgUtil getResponseMsgUtil() {
		return responseMsgUtil;
	}
	@Autowired
	public void setResponseMsgUtil(ResponseMsgUtil responseMsgUtil) {
		this.responseMsgUtil = responseMsgUtil;
	}
	
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	@Autowired
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public Object getModel() {
		return sale;
	}

	public SaleServ getSaleServ() {
		return saleServ;
	}
	@Autowired
	public void setSaleServ(SaleServ saleServ) {
		this.saleServ = saleServ;
	}
	
	public OperateUtil getOperateUtil() {
		return operateUtil;
	}
	@Autowired
	public void setOperateUtil(OperateUtil operateUtil) {
		this.operateUtil = operateUtil;
	}
	public SaleDetailServ getSaleDetailServ() {
		return saleDetailServ;
	}
	@Autowired
	public void setSaleDetailServ(SaleDetailServ saleDetailServ) {
		this.saleDetailServ = saleDetailServ;
	}

	public Sale getSale() {
		return sale;
	}

	public void setSale(Sale sale) {
		this.sale = sale;
	}

	public SaleFeeServ getSaleFeeServ() {
		return saleFeeServ;
	}
	@Autowired
	public void setSaleFeeServ(SaleFeeServ saleFeeServ) {
		this.saleFeeServ = saleFeeServ;
	}

	public List<SaleFee> getSaleFeeList() {
		return saleFeeList;
	}

	public void setSaleFeeList(List<SaleFee> saleFeeList) {
		this.saleFeeList = saleFeeList;
	}

	public List<SaleDetail> getSaleDetailList() {
		return saleDetailList;
	}

	public void setSaleDetailList(List<SaleDetail> saleDetailList) {
		this.saleDetailList = saleDetailList;
	}

}
